package abcy.cloud.blogcommon.resp;



import abcy.cloud.blogcommon.enums.StatusCodeEnum;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class ResponseEntity<T> implements Serializable {

    private static final long serialVersionUID  = 5263366145509571719L;


    protected Integer code;


    protected String message;


    protected T data;


    public static <T>  ResponseEntity<T> success() {
        return new ResponseEntity<>(StatusCodeEnum.SUCCESS.getCode(), StatusCodeEnum.SUCCESS.getMessage(),null);
    }

    public static <T> ResponseEntity<T> success(String message){
        return new ResponseEntity<>(StatusCodeEnum.SUCCESS.getCode(), message,null);
    }

    public static <T> ResponseEntity<T> success(T data){
        return new ResponseEntity<>(StatusCodeEnum.SUCCESS.getCode(), StatusCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> ResponseEntity<T> success(String message, T data){
        return new ResponseEntity<>(StatusCodeEnum.SUCCESS.getCode(), message,data);
    }

    public static <T>  ResponseEntity<T> fail() {
        return new ResponseEntity<>(StatusCodeEnum.FAIL.getCode(), StatusCodeEnum.FAIL.getMessage(),null);
    }

    public static <T> ResponseEntity<T> fail(Integer code, String message){
        return new ResponseEntity<>(code, message,null);
    }

    public static <T> ResponseEntity<T> fail(Integer code, String message, T data){
        return new ResponseEntity<>(code, message, data);
    }

    public static <T> ResponseEntity<T> invaildParam(String message){
        return new ResponseEntity<>(StatusCodeEnum.PARAM_INVALID.getCode(), message,null);
    }

    /*public static ResponseEntity fail(String 同一个账号只能发起好友申请3次) {
    }*/

    public static <T> ResponseEntity<T> fail(String message) {
        return new ResponseEntity<T>(StatusCodeEnum.FAIL.getCode(), message, null);
    }

    public boolean isSuccess() {
        return StatusCodeEnum.SUCCESS.getCode() == this.getCode();
    }

    @Override
    public String toString() {
        return "{" +
                "code:" + code +
                ",message:" + message +
                (null != data ? (",data:" + JSONObject.toJSONString(data)) : "") +
                ')';
    }
}
